package com.hu.auth.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hu.auth.common.config.WebInterceptor;
import com.hu.auth.common.result.R;
import com.hu.auth.common.utils.JwtUtil;
import com.hu.auth.common.utils.MD5;
import com.hu.auth.model.User;
import com.hu.auth.model.po.LoginPo;
import com.hu.auth.service.UserService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

// @CrossOrigin //解决跨域问题
@Api(tags = "登录模块")
@RestController
@RequestMapping("/user")
public class LoginController {

    @Resource
    UserService userService;

    @PostMapping("/login")
    public R login(@RequestBody LoginPo po) {
        User user = userService.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getUsername, po.getUsername())
                .eq(User::getPassword, MD5.encrypt(po.getPassword())));
        if (user == null) {
            return R.fail().message("账号或密码发生错误");
        }
        Map<String, String> map = new HashMap<>();
        // map.put("token", JwtUtil.createToken(user.getId(), String.join(",", userService.getUserPerms(user.getId()))));
        map.put("token", JwtUtil.createToken(user.getId()));
        return R.ok(map);
    }

    @GetMapping("/info")
    public R info() {
        return R.ok(userService.info(WebInterceptor.getUserId()));
    }

    @GetMapping("/test")
    public R test() {
        return R.ok().extra("123");
    }


}